package com.lingying.caiwudailikehudaun.controller.wuPinJiaoJie;

import com.alibaba.fastjson.JSONObject;
import com.lingying.caiwudailikehudaun.domain.wuPinJiaoJie.WuPinJiaoJieBean;
import com.lingying.caiwudailikehudaun.mapper.wuPinJiaoJIe.WuPinJiaoJieMapper;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/wuPinJiaoJie")
public class WuPinJiaoJieController {


    @Resource
    WuPinJiaoJieMapper wuPinJiaoJieMapper;

    @Resource
    HttpServletRequest httpServletRequest;
    /**
     * 查询物品叫汇总的提交人信息
     * 桑满
     * @param
     * @return
     */
    @RequestMapping(value = "/selectTianXieRen", method = RequestMethod.GET)
    public Object selectTianXieRen() {
        JSONObject object = new JSONObject();
        Integer suoShuDaiLiShangId = (Integer) httpServletRequest.getSession().getAttribute("jiGouId");
        List<Map<Integer, String>> selectWuPinJiaoJieList = wuPinJiaoJieMapper.selectTianXieRen(suoShuDaiLiShangId);
        if (selectWuPinJiaoJieList.size() > 0) {
            object.put("data", selectWuPinJiaoJieList);
            object.put("code", 200);
            object.put("msg", "查询成功");
        } else {
            object.put("code", 500);
            object.put("msg", "无数据");
        }
        return object;
    }

    /**
     * 页面回显物品交接信息
     * 桑满20210603
     * @return
     */
    @GetMapping(value = "/selectWuPinJiaoJie")
    public Object selectWuPinJiaoJie(
            @RequestParam(value = "jiaoJieFangShi", required = false) String jiaoJieFangShi,
            @RequestParam(value = "sendUser", required = false) String sendUser,
            @RequestParam(value = "acceptUser", required = false) String acceptUser,
            @RequestParam(value = "tianXieUserId", required = false) Integer tianXieUserId,
            @RequestParam(value = "jiaoJieShiJianBegin", required = false) String jiaoJieShiJianBegin,
            @RequestParam(value = "jiaoJieShiJianEnd", required = false) String jiaoJieShiJianEnd,
            @RequestParam(value = "page", required = false, defaultValue = "0") Integer page,
            @RequestParam(value = "limit", required = false, defaultValue = "10") Integer limit) {
        JSONObject object = new JSONObject();
        Integer pages = (page - 1) * limit;

        //获取到当前选中公司的做账申报id  并作为查询条件
        Integer daiLiShangCid = (Integer) httpServletRequest.getSession().getAttribute("daiLiShangCid");

        String jiaoJieType="客户给本公司";

//        Integer suoShuDaiLiShangId = (Integer) httpServletRequest.getSession().getAttribute("jiGouId");
        List<WuPinJiaoJieBean> selectWuPinJiaoJieList = wuPinJiaoJieMapper.selectWuPinJiaoJie(daiLiShangCid,jiaoJieFangShi,
                jiaoJieType, sendUser, acceptUser, tianXieUserId, jiaoJieShiJianBegin, jiaoJieShiJianEnd, pages, limit);
        //总个数
        Integer count = wuPinJiaoJieMapper.selectWuPinJiaoJieCount(daiLiShangCid, jiaoJieFangShi,
                jiaoJieType, sendUser, acceptUser, tianXieUserId, jiaoJieShiJianBegin, jiaoJieShiJianEnd);
        if (selectWuPinJiaoJieList.size() > 0) {
            object.put("data", selectWuPinJiaoJieList);
            object.put("count", count);
            object.put("code", 0);
            object.put("msg", "查询成功");
        } else {
            object.put("code", 500);
            object.put("msg", "无数据");
        }
        return object;
    }

    /**
     * 查看当条物品交接审批状态
     *
     * @param shenPiId
     * @return
     */
    @PostMapping(value = "/selectShenPiZhuangTai")
    public Object selectShenPiZhuangTai(Integer shenPiId) {
        JSONObject object = new JSONObject();
        Integer count = wuPinJiaoJieMapper.selectShenPiZhuangTai(shenPiId);
        if (count > 0) {
            object.put("code", 200);
            object.put("msg", "查询成功");
            object.put("data", count);
        } else {
            object.put("code", 500);
            object.put("msg", "查询失败");
        }
        return object;
    }

    /**
     * * @author 刘文浩
     * * @create 2020-10-13  14：22
     * 根据传入物品交接ID拿到物品交接表当行数据
     *
     * @param shenPiId
     * @return
     */
    @PostMapping(value = "/selectWuPinJiaoJieXinXi")
    public Object selectWuPinJiaoJieXinXi(Integer shenPiId, Integer shenPiType) {
        JSONObject object = new JSONObject();
        Map<String, Object> count = wuPinJiaoJieMapper.selectWuPinTable(shenPiId);
        System.out.println(count);
        //审批人
        List<Map<Integer,String>> yiJiShenPi = wuPinJiaoJieMapper.selectShenPiXinXi(shenPiId, shenPiType);
        //抄送人
        List<String> chaoSongRen = wuPinJiaoJieMapper.selectChaoSongRen(shenPiId, shenPiType);
        //当前操作人ID
        Integer userId = (Integer) httpServletRequest.getSession().getAttribute("userId");
        //当前登陆人的所属代理商
        Integer suoShuDaiLiShangId = (Integer) httpServletRequest.getSession().getAttribute("jiGouId");
        Integer companyId = (Integer) httpServletRequest.getSession().getAttribute("jiGouId");
        String userName = (String) httpServletRequest.getSession().getAttribute("userName");
        if (yiJiShenPi != null) {
            object.put("yiJiShenPi", yiJiShenPi);
        }
        if (chaoSongRen != null) {
            object.put("chaoSongRen", chaoSongRen);
        }
        if (count != null) {
            object.put("code", 200);
            object.put("msg", "查询成功");
            object.put("data", count);
        } else {
            object.put("code", 500);
            object.put("msg", "查询失败");
        }
        return object;
    }

    /**
     * @param wupinjiaojieid
     * @return
     * @author 刘文浩
     * @create 2020-10-13  14：22
     * 查询提交人
     */
    @PostMapping(value = "/selectTiJiaoRen")
    public Object selectTiJiaoRen(Integer wupinjiaojieid) {
        JSONObject object = new JSONObject();
        WuPinJiaoJieBean wuPinJiaoJieList = wuPinJiaoJieMapper.selectTiJiaoRen(wupinjiaojieid);
        if (wuPinJiaoJieList != null) {
            object.put("code", 200);
            object.put("msg", "查询成功");
            object.put("yijiandata", wuPinJiaoJieList);
        } else {
            object.put("code", 500);
            object.put("msg", "查询失败");
        }
        return object;
    }

    /**
     * 查询当审批记录
     *
     * @param shenPiId
     * @return
     * @author 刘文浩
     * @create 2020-10-21 17.34
     */
    @PostMapping(value = "/selectshenpiyijian")
    public Object selectshenpiyijian(Integer shenPiId, Integer shenPiType) {
        JSONObject object = new JSONObject();
        //查询到所有审批人的审批状态
        List<Map<String, Object>> count = wuPinJiaoJieMapper.selectshenpiyijian(shenPiId, shenPiType);
        if (count != null) {
            object.put("code", 200);
            object.put("msg", "查询成功");
            object.put("yijiandata", count);
        } else {
            object.put("code", 500);
            object.put("msg", "查询失败");
        }
        return object;
    }

    /**
     * 获取当前物品交接的拒绝审批意见
     *
     * @param shenPiId
     * @param shenPinRenId
     * @return
     */
    @PostMapping(value = "/selectjujueyijian")
    public Object selectjujueyijian(Integer shenPiId, Integer shenPinRenId, Integer shenPiType) {
        JSONObject object = new JSONObject();
        //获取当前物品交接的拒绝审批意见
        String count = wuPinJiaoJieMapper.selectjujuedata(shenPiId, shenPinRenId, shenPiType);
        if (count != null) {
            object.put("code", 200);
            object.put("msg", "查询成功");
            object.put("yijiandata", count);
        } else {
            object.put("code", 500);
            object.put("msg", "查询失败");
        }
        return object;
    }

    /**
     * @param wupinjiaojieid
     * @return
     * @author 刘文浩
     * @create 2020-10-13  14：22
     * 根据传入物品交接ID找到物品交接表对应ID的文件路径
     */
    @PostMapping(value = "/selectfilepath")
    public Object selectfilepath(Integer wupinjiaojieid) {
        JSONObject object = new JSONObject();
        List<Map> count = wuPinJiaoJieMapper.selectfilepath(wupinjiaojieid);
        Integer userId = (Integer) httpServletRequest.getSession().getAttribute("userId");
        Integer companyId = (Integer) httpServletRequest.getSession().getAttribute("jiGouId");
        String userName = (String) httpServletRequest.getSession().getAttribute("userName");
        Integer suoShuDaiLiShangId = (Integer) httpServletRequest.getSession().getAttribute("jiGouId");
        List list = new ArrayList();
        for (Map path : count) {
            if (path.get("filePath") == null || path.get("filePath").equals("")) {
            } else {
                list.add("文件路径：" + path.get("filePath"));
            }
            if (path.get("weiXinPath") == null || path.get("weiXinPath").equals("")) {
            } else {
                list.add("微信文件：" + path.get("weiXinPath"));
            }
            if (path.get("yinPinPath") == null || path.get("yinPinPath").equals("")) {
            } else {
                list.add("音频文件：" + path.get("yinPinPath"));
            }
        }
        //caoZuoRiZhiMapper.addBeiZhu(companyId,userId,"查询","交接审批",userName+"：查询交接ID为"+wupinjiaojieid+"的物品交接信息的"+count.size()+"条文件路径:"+list,suoShuDaiLiShangId);
        if (count != null) {
            object.put("code", 200);
            object.put("msg", "查询成功");
            object.put("data", count);
        } else {
            object.put("code", 500);
            object.put("msg", "查询失败");
        }
        return object;
    }
}
